﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.General;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.Controls
{
    public partial class GlobalNodeSelectionControl : VehicleControl
    {
        #region Fields

        public event EventHandler<Node> NodeValidated;
        public event EventHandler<Node> SelectedNodeChanged;

        #endregion

        #region Properties

        #endregion

        #region CTORs

        public GlobalNodeSelectionControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        public void Reload()
        {
            list.DropZone.Controls.Clear();
            if (Vehicle == null) return;

            list.Visible = false;
            SuspendLayout();

            foreach (var file in Vehicle)
            {
                foreach (var part in file)
                {
                    /* Create groupbox */
                    var group = new CollapsibleBox() { Text = part.Description };

                    /* Create nodes control */
                    var nodeControl = new NodeSelectionControl2()
                    {
                        VehiclePart = part,
                        Dock = DockStyle.Fill
                    };

                    /* Map node selection events */
                    nodeControl.SelectedNodeChanged += (s, node) =>
                    {
                        if (SelectedNodeChanged != null)
                            SelectedNodeChanged(this, node);
                    };
                    nodeControl.NodeValidated += (s, node) =>
                    {
                        if (NodeValidated != null)
                            NodeValidated(this, node);
                    };

                    group.DropZone.Controls.Add(nodeControl);
                    list.DropZone.Controls.Add(group);

                    group.BringToFront();
                }
            }

            ResumeLayout(true);
            list.Visible = true;
        }

        protected override void OnVehicleChanged()
        {
            list.Text = Vehicle != null ? string.Format("{0} nodes", Vehicle.Label) : "Nodes";

            Reload();
            base.OnVehicleChanged();
        }

        #endregion
    }
}
